#include <queue>
#include <nice2d/action.h>
#include <nice2d/object.h>
#include <nice2d/entity.h>

class ObjectData
{
public:
    std::queue<ActionPointer> actions;

    void update(float deltaTime)
    {
        if (!actions.empty()) {
            auto current = actions.front();
            if (current->getStatus() == ActionStatus_Idel)
                current->start();
            current->update(deltaTime);
            if (current->getStatus() == ActionStatus_Done)
                actions.pop();
        }
    }
};

Object::Object():
    data(new ObjectData())
{
}

Object::~Object()
{
}

void Object::runAction(ActionPointer action)
{
    if (action) {
        if (action->getOwner().expired())
            action->setOwner(shared_from_this());
        data->actions.push(action);
    }
}

void Object::stopCurrentAction()
{
    if (!data->actions.empty()) {
        data->actions.front()->stop();
        data->actions.pop();
    }
}

void Object::stopAllActions()
{
    while (!data->actions.empty()) {
        data->actions.front()->stop();
        data->actions.pop();
    }
}

void Object::update(float deltaTime)
{
    updateComponent(deltaTime);
    data->update(deltaTime);
}

IMPLEMENT_BASE_OBJECT(Object)
